package org.edu.controller;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;
import org.edu.bean.TmBankinfo;
import org.edu.bean.TmPersoninfo;
import org.edu.common.PageView;
import org.edu.common.QueryResult;
import org.edu.service.TmBankinfoService;
import org.edu.service.TmPersoninfoService;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class TmBankInfoController extends ActionSupport{
	@Resource 
	private TmBankinfoService tmBankinfoService;
	
	@Resource
	private TmPersoninfoService tmPersoninfoService;
	
	private String pcode;
	private String message;
	private TmBankinfo entity;
	private TmPersoninfo tmPersonInfo;
	private int page = 1;
	
	@JSON(name="tmPersonInfo")
	public TmPersoninfo getTmPersonInfo() {
		return tmPersonInfo;
	}

	public void setTmPersonInfo(TmPersoninfo tmPersonInfo) {
		this.tmPersonInfo = tmPersonInfo;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String getPcode() {
		return pcode;
	}

	public void setPcode(String pcode) {
		this.pcode = pcode;
	}

	public TmBankinfo getEntity() {
		return entity;
	}

	public void setEntity(TmBankinfo entity) {
		this.entity = entity;
	}
	@Override
	public String execute() throws Exception {
		try {
			tmBankinfoService.save(entity);
			addActionMessage("银行资料信息备案成功【"+entity.getPcode()+"】");
		} catch (Exception e) {
			addActionMessage("银行资料信息备案失败【"+entity.getPcode()+"】");
			e.printStackTrace();
		}
		return "message";
	}
	public String check() throws Exception {
		TmPersoninfo personinfo = tmPersoninfoService.findByCardNo(pcode);
		if(personinfo == null){
			this.message = "该纳税人未进行个人信息登记，不能进行银行资料信息备案";
		}else{
			TmBankinfo bankinfo = tmBankinfoService.findByPCode(pcode);
			if(bankinfo != null){
				this.message = "纳税人识别码为【"
					+personinfo.getCardno()
					+"】已经进行银行资料信息备案，不能重复办理该业务";
			}else{
				this.tmPersonInfo = personinfo;
				return "check2";
			}
		}
		return "check";
	}
	public String list() throws Exception {
		PageView<TmBankinfo> pageView = new PageView<TmBankinfo>(20,this.page);
		QueryResult<TmBankinfo> qr = tmBankinfoService.getQueryResult(pageView.getFirstResult()
				,pageView.getMaxresult());
		pageView.setQueryResult(qr);
		ServletActionContext.getRequest().setAttribute("pageView", pageView);
		return SUCCESS;
	}
	
}
